#
#@Author: klj
#@Date: 2023-12-14 10:59:21 
#@Last Modified by:   klj
#@Last Modified time: 2024-03-19 10:59:21 
#
#
# makefile for RISCV-GNU-TOOLCHAIN
#
# please follow the steps below!
#
# !!! modify the PATH_TO_INSTALL firstly
# use 'make prerequisites_ubuntu' to install prerequisites, if centos, use 'make prerequisites_centos'
# use 'make clone' to clone full toolchain at first time
# use 'make update' to update repo and all submodules
# use 'make build' to build the toolchain
# use 'make addpath_manual' to get the method about how to modify the ~/.bashrc file
# use 'source ~/.bashrc' or reopen the terminal to enable the environment variables
# use 'cd $RISCV/bin' and then 'ls' to check the environment variables works well, the the content listed are all useful to compile
# compile your code to check the toolchain works well

#########################     PARAMETER    #######

PATH_TO_INSTALL=/home/tk/App/riscv-gnu-toolchain/
ARCH_EXTENSION=rv64gc_zba_zbb_zbc_zbs			#注意这里的编译指令和后面使用-march时启用指令关联密切，最好后面用到什么这里配置什么

###########################################
.ONESHELL:
.PHONY: update
update:
	cd riscv-gnu-toolchain	
	git pull				
	git submodule update --init --recursive
	git status

.PHONY: clone
clone:
	make downloadiscassh
	make replace_llvm_url_to_th_mirror
	bash riscv-gnu-toolchain.sh 

.ONESHELL:
.PHONY: build
build:
	cd riscv-gnu-toolchain/
	mkdir build
	cd build
	../configure --prefix=$(PATH_TO_INSTALL) --with-arch=$(ARCH_EXTENSION)
	time make -j`nproc`			# newlib (riscv-unknown-elf-)
	time make linux -j`nproc`	# linux (riscv-unknown-linux-gnu-)

.PHONY: addpath
addpath:	
	export RISCV=$(PATH_TO_INSTALL) 
	export PATH=$(RISCV)/bin:$(PATH)
	
.PHONY: addpath_manual
addpath_manual:
	@echo "打开bashrc(vi ~/.bashrc)，将下面两行复制进去:"
	@echo "export RISCV=$(PATH_TO_INSTALL)"
	@echo 'export PATH=$$RISCV/bin:$$PATH'
############################################# Prerequisites
# see: https://github.com/riscv-collab/riscv-gnu-toolchain#prerequisites
.PHONY:prerequisites_ubuntu
prerequisites_ubuntu:
	sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev

.PHONY:prerequisites_centos
prerequisites_centos:
	sudo yum install autoconf automake python3 libmpc-devel mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel expat-devel


############################################
.PHONY: downloadiscassh
downloadiscassh:
	curl https://mirror.iscas.ac.cn/riscv-toolchains/git/riscv-collab/riscv-gnu-toolchain.sh  > riscv-gnu-toolchain.sh 

.PHONY: clonefromiscas
clonefromiscas:
	curl https://mirror.iscas.ac.cn/riscv-toolchains/git/riscv-collab/riscv-gnu-toolchain.sh  > riscv-gnu-toolchain.sh 
	bash riscv-gnu-toolchain.sh 

.PHONY:replace_llvm_url_to_th_mirror
replace_llvm_url_to_th_mirror:
	sed -i 's/https:\/\/mirror\.iscas\.ac\.cn\/riscv-toolchains\/git\/riscv-collab\/riscv-gnu-toolchain\/llvm\.git/https:\/\/mirrors\.tuna\.tsinghua\.edu\.cn\/git\/llvm-project\.git/g' riscv-gnu-toolchain.sh 

.ONESHELL:
.PHONY: clonellvmfromth
clonellvmfromth:
	git clone https://mirrors.tuna.tsinghua.edu.cn/git/llvm-project.git riscv-gnu-toolchain/llvm
	cd riscv-gnu-toolchain
	git submodule init llvm
	git submodule update llvm
